package com.fm.service;

import com.fm.entity.AbstractMyBatisEntity;
import com.fm.mapper.MBaseMapper;
import com.fm.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.domain.*;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * 基础的service
 * Created by Administrator on 2018/7/30 0030.
 */
public abstract class MBaseService<T extends AbstractMyBatisEntity, ID extends Serializable, M extends MBaseMapper<T, ID>> {

    @Autowired
    private M myBatisDao;

    public M getMyBatisDao() {
        return myBatisDao;
    }

    public void setMyBatisDao(M myBatisDao) {
        this.myBatisDao = myBatisDao;
    }

    /**
     * 根据id删除
     * @param
     */
    @Transactional
    public void deleteById(ID t) throws DataAccessException {
        beforeDelete(t);
        this.myBatisDao.deleteById(t);
        afterDelete(t);
    }

    public void beforeDelete(ID t){}
    public void afterDelete(ID t){}

    @Transactional
    public T insert(T t) throws DataAccessException{
        beforeInsert(t);
        t = this.myBatisDao.save(t);
        afterInsert(t);
        return t;
    }


    @Transactional
    public T save(T t) throws DataAccessException{
        t = this.myBatisDao.save(t);
        return t;
    }

    public void beforeInsert(T t){}
    public void afterInsert(T t){}

    @Transactional
    public T update(T t) throws DataAccessException{
        beforeUpdate(t);
//        Example<T> example = Example.of(t);
//        Optional<T> oldt = myBatisDao.findOne(example);
//        UpdateUtil.copyNullProperties(t, oldt.get());
        t = this.myBatisDao.save(t);
        afterUpdate(t);
        return t;
    }
    public void beforeUpdate(T t){}
    public void afterUpdate(T t){}
    /**
     * 根据id获得一个实体类
     * @return
     */
    public Optional<T> selectOneByExample(ID id) throws DataAccessException{
        Optional<T> entity = myBatisDao.findById(id);
        return entity;
    }

    //分页查询
    public Page<T> queryPage(T t, Integer pageNum, Integer pageSize, String orderBy) {
        Pageable pageable;
        if (orderBy!=null) {
            String ods [] = orderBy.split(" ");
            Sort sort;
            if (ods.length>0){
                sort = new Sort(Sort.Direction.DESC,ods[0]);
            }else {
                sort = new Sort(Sort.Direction.ASC,ods[0]);
            }
            pageable =new PageRequest(pageNum, pageSize, sort);
        }else {
            pageable =new PageRequest(pageNum, pageSize);
        }
        Example<T> example = Example.of(t);
        Page<T> entitys = this.myBatisDao.findAll(example, pageable);
        return entitys;
    }

    public List<T> selectByExample(T t) {
        Example<T> example = Example.of(t);
        return this.myBatisDao.findAll(example);
    }

    public Integer selectCount(T t) {
        Example<T> example = Example.of(t);
        return this.myBatisDao.findAll(example).size();
    }
}
